package Risk.Moteur;

//~--- JDK imports ------------------------------------------------------------

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *  Class description
 *     @version    1.0, 07/12/26
 *   @author     Alexandre LIGUORI
 */
public class Plateau {
    private ArrayList<Continent> continents;

    /**
     * Constructs ...
     *
     */
    public Plateau() {
        this.continents = new ArrayList<Continent>();
    }

    public ArrayList<Continent> getContinents() {
        return continents;
    }

    private boolean ajouteContinent(Continent val) {
        return this.continents.add(val);
    }

    public void initialisationContinents(String fic) {
        FileReader      fl    = null;
        BufferedReader  bf    = null;
        String          ligne = null;
        StringTokenizer stok;
        String          continent = "";
        int             renfort   = 0;
        Continent       continent_TMP;

        try {
            File file = new File(fic);

            fl    = new FileReader(file);
            bf    = new BufferedReader(fl);
            ligne = bf.readLine();

            while (ligne != null) {
                stok          = new StringTokenizer(ligne, " ");
                continent     = stok.nextToken();
                renfort       = Integer.parseInt(stok.nextToken());
                continent_TMP = new Continent(continent, renfort, this);
                continent_TMP.initialisationPays("." + File.separator + "src" + File.separator + "Risk"
                                                 + File.separator + "Moteur" + File.separator + "donnee"
                                                 + File.separator + "pays_continents.prn");
                ajouteContinent(continent_TMP);
                ligne = bf.readLine();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                bf.close();
                fl.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void intialiseFrontiere(String fic) {
        for (Continent conti : getContinents()) {
            for (Pays p : conti.getPaysContinent()) {
                p.initialiseVoisins(fic);
            }
        }
    }
}
